package com.ezcoding.controller;


import com.ezcoding.service.UserService;
import com.ezcoding.utils.StatusUtil;
import com.ezcoding.utils.Tools;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.tx.Tx;

@Before(Tx.class)
public class UserController extends Controller {

    /**
     * 用户注册
     */
    public void register() {
        String username = getPara("username");
        String password = getPara("password");
        String inviteCode = getPara("inviteCode");

        renderJson(UserService.getInstance().register(username, password, inviteCode));
    }

    /**
     * 获取手机验证码
     */
    public void sendMessage() {
        String mobile = getPara("mobile");

        renderJson(UserService.getInstance().sendMessage(mobile));
    }

    /**
     * 用户登录
     */
    public void login() {
        String username = getPara("username");
        String password = getPara("password");

        renderJson(UserService.getInstance().login(username, password));
    }

    /**
     * 修改密码
     */
    public void changePassword() {
        String username = getPara("username");
        String oldPassword = getPara("oldPassword");
        String newPassword = getPara("newPassword");
        // 0:登录密码 1:操作密码
        String passwordType = getPara("passwordType");

        renderJson(UserService.getInstance().changePassword(username, oldPassword, newPassword, passwordType));
    }

    /**
     * 更新用户信息、公司信息、商品信息
     */
    public void updateUserInfo() {
        String username = getPara("username");

        renderJson(UserService.getInstance().updateUserInfo(username));
    }

    public void updateUser() {
        String username = getPara("username");
        String nickName = getPara("nickName");
        String position = getPara("position");
        String tel = getPara("tel");
        String mobile = getPara("mobile");
        String QQ = getPara("QQ");
        String wechat = getPara("wechat");

        renderJson(UserService.getInstance().updateUser(username, nickName, position, tel, mobile, QQ, wechat));
    }

    public void download() {
        String fileName = getPara("fileName");

        System.out.println(fileName);
        if (Tools.checkParaIsInValidate(fileName)) {
            renderJson(StatusUtil.getFailedStatusWithMessage("参数无效！"));
            return;
        }

        switch (fileName) {
            case "client.xls":
                renderFile("客户导入模板.xls");
                break;
            case "giver.xls":
                renderFile("供应商导入模板.xls");
                break;
            case "product.xls":
                renderFile("商品导入模板.xls");
                break;
            default:
                renderJson(StatusUtil.getFailedStatusWithMessage("文件不存在！"));
                break;
        }
    }

    /**
     * 重置密码
     */
    public void findPassword() {
        String username = getPara("username");
        String password = getPara("password");

        renderJson(UserService.getInstance().findPassword(username, password));
    }
}
